Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: check arguments mismatch of array.sorted_with_compare() (fix #22327) #22328

Merged
merged 3 commits into from
Sep 27, 2024

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Sep 27, 2024

This PR check arguments mismatch of array.sorted_with_compare() (fix #22327).

  • Check arguments mismatch of array.sorted_with_compare().
  • Add test.
fn main() {
	names := ['aaa', 'bbb', 'ccc']

	name1 := names.sorted_with_compare(sort_by_file_base)
	println(name1)

	name2 := names.sorted_with_compare(22)
	println(name2)
}

fn sort_by_file_base(a string, b string) int {
	return int(a > b)
}

PS D:\Test\v\tt1> v run .
tt1.v:11:24: error: sorted_with_compare callback function parameter `a` with type `string` should be `&string`
    9 | }
   10 | 
   11 | fn sort_by_file_base(a string, b string) int {
      |                        ~~~~~~
   12 |     return int(a > b)
   13 | }
tt1.v:11:34: error: sorted_with_compare callback function parameter `b` with type `string` should be `&string`
    9 | }
   10 | 
   11 | fn sort_by_file_base(a string, b string) int {
      |                                  ~~~~~~
   12 |     return int(a > b)
   13 | }
tt1.v:4:37: error: cannot use `fn (string, string) int` as `fn (voidptr, voidptr) int` in argument 1 to `[]string.sorted_with_compare`
    2 |     names := ['aaa', 'bbb', 'ccc']
    3 | 
    4 |     name1 := names.sorted_with_compare(sort_by_file_base)
      |                                        ~~~~~~~~~~~~~~~~~
    5 |     println(name1)
    6 |
Details: expected argument 1 to be a pointer, but the passed argument 1 is NOT a pointer
tt1.v:7:37: error: cannot use `int literal` as `fn (voidptr, voidptr) int` in argument 1 to `[]string.sorted_with_compare`
    5 |     println(name1)
    6 | 
    7 |     name2 := names.sorted_with_compare(22)
      |                                        ~~
    8 |     println(name2)
    9 | }

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman spytheman merged commit 372a402 into vlang:master Sep 27, 2024
78 checks passed
@yuyi98 yuyi98 deleted the check_sort_with_compare_arg branch September 28, 2024 00:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to check arguments mismatch errors of array.sorted_with_compare()
2 participants